import { getData } from '@vimi/utils-api'

const state = {
  letterType: {},
}

const getters = {
  letterType: state => state.letterType,
}

const actions = {
  async getLetterType({ commit }, { force = false } = {}) {
    if (!force && Object.keys(state.letterType).length !== 0) {
      return state.letterType
    }

    const res = await getData('credit', 'system_enum', {
      'enum_field.eq': 'guarantee_project.project_type',
    })

    const data = (res?.data ?? []).reduce((s, n) => {
      s[n.enum_key] = n.enum_value
      return s
    }, {})
    commit('letterType', data)
    return data
  },
}

const mutations = {
  letterType: (state, payload) => (state.letterType = payload),
}

export default {
  state,
  getters,
  actions,
  mutations,
}
